<template>
  <div class="common-container">
    <!-- 操作区域 -->
    <el-card class="operation-card">
      <div class="operation-buttons">
        <el-button type="primary" @click="handleAdd">
          <el-icon><Plus /></el-icon> 新增
        </el-button>
        <el-button type="danger" :disabled="!selectedRows.length" @click="handleBatchDelete">
          <el-icon><Delete /></el-icon> 批量删除
        </el-button>
        <el-button type="success" @click="handleExport">
          <el-icon><Download /></el-icon> 导出
        </el-button>
      </div>
    </el-card>

    <!-- 表格区域 -->
    <el-card class="table-card">
      <vxe-table
        v-model:data="tableData"
        :loading="loading"
        @checkbox-change="handleSelectionChange"
        @checkbox-all="handleSelectionChange"
      >
        <vxe-column type="checkbox" width="60" />
        <vxe-column field="id" title="ID" width="80" />
        <!-- 根据需要添加更多列 -->
        <vxe-column title="操作" width="200" fixed="right">
          <template #default="{ row }">
            <el-button type="primary" link @click="handleEdit(row)">
              <el-icon><Edit /></el-icon> 编辑
            </el-button>
            <el-button type="danger" link @click="handleDelete(row)">
              <el-icon><Delete /></el-icon> 删除
            </el-button>
          </template>
        </vxe-column>
      </vxe-table>

      <vxe-pager
        v-model:current-page="pagination.currentPage"
        v-model:page-size="pagination.pageSize"
        :total="pagination.total"
        :page-sizes="[10, 20, 30, 50]"
        @page-change="handleCurrentChange"
        @size-change="handleSizeChange"
      />
    </el-card>

    <!-- 表单对话框 -->
    <el-dialog
      :title="form.isAdd ? '新增' : '编辑'"
      v-model="dialogVisible"
      width="500px"
      destroy-on-close
    >
      <el-form
        ref="formRef"
        :model="form"
        :rules="rules"
        label-width="100px"
        style="max-width: 460px"
      >
        <!-- 根据需要添加表单项 -->
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="submitForm">确认</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import {
  Plus,
  Edit,
  Delete,
  Download
} from '@element-plus/icons-vue'
import { ElMessage, ElMessageBox } from 'element-plus'

// 定义组件名
defineOptions({
  name: 'ListPageTemplate'
})

// 表格数据
const tableData = ref([])
const loading = ref(false)
const selectedRows = ref([])

// 分页信息
const pagination = reactive({
  currentPage: 1,
  pageSize: 10,
  total: 0
})

// 对话框控制
const dialogVisible = ref(false)
const formRef = ref(null)
const form = reactive({
  id: '',
  name: '',
  // 根据需要添加更多字段
  isAdd: true
})

// 表单校验规则
const rules = {
  name: [
    { required: true, message: '请输入名称', trigger: 'blur' }
  ]
  // 根据需要添加更多规则
}

// 获取数据列表
const getDataList = () => {
  loading.value = true
  // 实际项目中这里会调用后端API
  setTimeout(() => {
    const mockData = [
      {
        id: 1,
        name: '示例数据1'
        // 根据需要添加更多字段
      },
      {
        id: 2,
        name: '示例数据2'
        // 根据需要添加更多字段
      }
    ]

    tableData.value = mockData
    pagination.total = mockData.length
    loading.value = false
  }, 500)
}

// 初始化数据
onMounted(() => {
  getDataList()
})

// 选择行变化
const handleSelectionChange = (rows) => {
  selectedRows.value = rows
}

// 分页大小变化
const handleSizeChange = (val) => {
  pagination.pageSize = val
  getDataList()
}

// 当前页变化
const handleCurrentChange = (val) => {
  pagination.currentPage = val
  getDataList()
}

// 新增
const handleAdd = () => {
  Object.keys(form).forEach(key => {
    form[key] = key === 'isAdd' ? true : ''
  })
  dialogVisible.value = true
}

// 编辑
const handleEdit = (row) => {
  Object.keys(form).forEach(key => {
    if (key in row) {
      form[key] = row[key]
    }
  })
  form.isAdd = false
  dialogVisible.value = true
}

// 删除
const handleDelete = (row) => {
  ElMessageBox.confirm(
    `确定要删除【${row.name}】吗？`,
    '警告',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }
  ).then(() => {
    // 实际项目中这里会调用删除API
    ElMessage.success('删除成功')
    getDataList()
  }).catch(() => {
    ElMessage.info('已取消删除')
  })
}

// 批量删除
const handleBatchDelete = () => {
  if (selectedRows.value.length === 0) {
    ElMessage.warning('请至少选择一条记录')
    return
  }

  ElMessageBox.confirm(
    `确定要删除选中的${selectedRows.value.length}条记录吗？`,
    '警告',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }
  ).then(() => {
    // 实际项目中这里会调用批量删除API
    ElMessage.success('批量删除成功')
    getDataList()
  }).catch(() => {
    ElMessage.info('已取消删除')
  })
}

// 导出
const handleExport = () => {
  ElMessage.success('导出成功')
}

// 提交表单
const submitForm = async () => {
  try {
    await formRef.value.validate()
    // 实际项目中这里会调用添加/更新API
    ElMessage.success(form.isAdd ? '添加成功' : '更新成功')
    dialogVisible.value = false
    getDataList()
  } catch (error) {
    console.error('表单验证失败', error)
  }
}
</script>

<style scoped>
/* 页面特有的样式可以在这里添加 */
</style>
